package com.iexbuy.ihg.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.TextView;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BaiduMapOptions;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.InfoWindow;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.model.LatLng;
import com.iexbuy.ihg.R;
import com.iexbuy.ihg.entity.AdObject;
import com.iexbuy.ihg.net.ServiceHelper;
import com.jingchen.pulltorefresh.PullToRefreshLayout;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.util.HashMap;

public class MapFragment extends BaseFragment {
	
	private static MapFragment homeFragmentInstent;

	@ViewInject(R.id.msgListLayout)
	private PullToRefreshLayout msgListRefreshLayout;
	@ViewInject(R.id.mapView)
	private MapView mMapView;

	private BaiduMap mBaiduMap;

	private static String name;
	private static String latitude;
	private static String longitude;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		homeFragmentInstent = this;
	}

	@Override
	protected View _onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_map, container, false);
		ViewUtils.inject(this, rootView);
		initView(rootView);
		return rootView;
	}

	private void initView(View rootView) {
		mBaseActivity.setTitle(name);
		mBaseActivity.setTitleLayoutVisibility(true);
		mBaseActivity.setHomeActivity(false);

		initMap(latitude, longitude);
	}

	private void initMap(String lat, String lng) {
		mBaiduMap = mMapView.getMap();
		mBaiduMap.getUiSettings().setCompassEnabled(false);
		mBaiduMap.animateMapStatus(MapStatusUpdateFactory.zoomTo(15));

		addMarker(lat, lng);
	}

	private void addMarker(String lat, String lng) {
		LatLng latLng = new LatLng(Float.valueOf(lat), Float.valueOf(lng));

		//添加点
		mBaiduMap.addOverlay(new MarkerOptions().position(latLng).icon(BitmapDescriptorFactory
				.fromResource(R.drawable.icon_gcoding))
				.zIndex(6));

		//把地图移到当前点
		MapStatus mMapStatus = new MapStatus.Builder().target(latLng).zoom(15).build();
		MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mMapStatus);
		mBaiduMap.setMapStatus(mMapStatusUpdate);
		TextView tv =  new TextView(mBaseActivity);
		tv.setText(name);
		InfoWindow infoWindow = new InfoWindow(tv,latLng,-50);
		mBaiduMap.showInfoWindow(infoWindow);
	}



	public static void setName(String name) {
		MapFragment.name = name;
	}

	public static void setLatitude(String latitude) {
		MapFragment.latitude = latitude;
	}

	public static void setLongitude(String longitude) {
		MapFragment.longitude = longitude;
	}

	/**
	 * @return the homeFragmentInstent
	 */
	public static MapFragment getHomeFragmentInstent() {
		return homeFragmentInstent;
	}

}
